package com.fht.service.promotion.impl;

import com.fht.entity.PromotionSpecialOffer;
import com.fht.from.promotionFrom;
import com.fht.from.promotionNowFrom;
import com.fht.mapper.promotion.PromotionSpecialOfferMapper;
import com.fht.service.promotion.PromotionSpecialOfferService;
import com.fht.util.DateTimeUtil;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import com.fht.vo.PromotionSpecialOfferNowVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class PromotionSpecialOfferServiceImpl implements PromotionSpecialOfferService {


    @Resource
    public PromotionSpecialOfferMapper promotionSpecialOfferMapper;

    @Override
    public LayUITableVO<List<PromotionSpecialOffer>> findPage(Map<String, Object> map) {

        promotionFrom from = (promotionFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));



        List<PromotionSpecialOffer> list = promotionSpecialOfferMapper.findPage(map);
        for (PromotionSpecialOffer promotionSpecialOffer : list) {
            conversion(promotionSpecialOffer);
        }

        PageInfo<PromotionSpecialOffer> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PromotionSpecialOffer>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;

    }

    @Override
    @Transactional
    public void update(PromotionSpecialOffer promotionSpecialOffer) {
        conversionContrary(promotionSpecialOffer);
        PromotionSpecialOffer old = promotionSpecialOfferMapper.selectByPrimaryKey(promotionSpecialOffer.getId());
        if ("".equals(old) || old == null){
            promotionSpecialOfferMapper.insertSelective(promotionSpecialOffer);
        }else {

            promotionSpecialOfferMapper.updateByPrimaryKeySelective(promotionSpecialOffer);
        }

    }

    @Override
    public PromotionSpecialOffer selectById(String id) {
        PromotionSpecialOffer promotionSpecialOffer = promotionSpecialOfferMapper.selectByPrimaryKey(id);
        conversion(promotionSpecialOffer);
        return promotionSpecialOffer;
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        promotionSpecialOfferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public LayUITableVO<List<PromotionSpecialOfferNowVo>> findNowPage(Map<String, Object> map) {

        promotionNowFrom from = (promotionNowFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        conversionTime(from);


        List<PromotionSpecialOfferNowVo> list = promotionSpecialOfferMapper.findNowPage(map);

        for (PromotionSpecialOfferNowVo promotionSpecialOfferNowVo : list) {
            conversionNow(promotionSpecialOfferNowVo);
        }

        PageInfo<PromotionSpecialOfferNowVo> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PromotionSpecialOfferNowVo>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    private void conversionNow(PromotionSpecialOfferNowVo promotionSpecialOfferNowVo) {

        promotionSpecialOfferNowVo.setOriginalPriceString(UnitConversionUtil.conversion(promotionSpecialOfferNowVo.getOriginalPrice()));

        if ("特价".equals(promotionSpecialOfferNowVo.getPromotionPattern())){
            promotionSpecialOfferNowVo.setSpecialPriceString(UnitConversionUtil.conversion(promotionSpecialOfferNowVo.getSpecialPrice()));
        }else if ("折扣".equals(promotionSpecialOfferNowVo.getPromotionPattern())){

            double value = promotionSpecialOfferNowVo.getOriginalPrice() * promotionSpecialOfferNowVo.getDiscount() / 100000.0;
            promotionSpecialOfferNowVo.setSpecialPriceString(String.valueOf(value));
        }else {
            promotionSpecialOfferNowVo.setSpecialPriceString(promotionSpecialOfferNowVo.getOriginalPriceString());
        }

        String period = "";
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodMon())){
            period += "一";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodTue())){
            period += "二";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodWed())){
            period += "三";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodThu())){
            period += "四";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodFri())){
            period += "五";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodSat())){
            period += "六";
        }
        if ("1".equals(promotionSpecialOfferNowVo.getPeriodSun())){
            period += "日";
        }

        promotionSpecialOfferNowVo.setPeriodString(period);

        promotionSpecialOfferNowVo.setStartDate(UnitConversionUtil.conversionDate(promotionSpecialOfferNowVo.getStartDate()));
        promotionSpecialOfferNowVo.setEndDate(UnitConversionUtil.conversionDate(promotionSpecialOfferNowVo.getEndDate()));
    }


    public void conversion (PromotionSpecialOffer promotionSpecialOffer){
        promotionSpecialOffer.setStartDate(UnitConversionUtil.conversionDate(promotionSpecialOffer.getStartDate()));
        promotionSpecialOffer.setEndDate(UnitConversionUtil.conversionDate(promotionSpecialOffer.getEndDate()));
        promotionSpecialOffer.setPreparedDate(UnitConversionUtil.conversionDate(promotionSpecialOffer.getPreparedDate()));

        promotionSpecialOffer.setPeriodMon(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodMon()));
        promotionSpecialOffer.setPeriodTue(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodTue()));
        promotionSpecialOffer.setPeriodWed(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodWed()));
        promotionSpecialOffer.setPeriodThu(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodThu()));
        promotionSpecialOffer.setPeriodFri(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodFri()));
        promotionSpecialOffer.setPeriodSat(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodSat()));
        promotionSpecialOffer.setPeriodSun(UnitConversionUtil.conversionTrue(promotionSpecialOffer.getPeriodSun()));


    }
    public void conversionContrary (PromotionSpecialOffer promotionSpecialOffer){
        promotionSpecialOffer.setStartDate(UnitConversionUtil.conversionDateContrary(promotionSpecialOffer.getStartDate()));
        promotionSpecialOffer.setEndDate(UnitConversionUtil.conversionDateContrary(promotionSpecialOffer.getEndDate()));
        promotionSpecialOffer.setPreparedDate(UnitConversionUtil.conversionDateContrary(promotionSpecialOffer.getPreparedDate()));

        promotionSpecialOffer.setPeriodMon(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodMon()));
        promotionSpecialOffer.setPeriodTue(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodTue()));
        promotionSpecialOffer.setPeriodWed(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodWed()));
        promotionSpecialOffer.setPeriodThu(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodThu()));
        promotionSpecialOffer.setPeriodFri(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodFri()));
        promotionSpecialOffer.setPeriodSat(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodSat()));
        promotionSpecialOffer.setPeriodSun(UnitConversionUtil.conversionTrueContrary(promotionSpecialOffer.getPeriodSun()));


    }

    //获取查询周期的开始，结束时间，今天为周几
    public void conversionTime(promotionNowFrom from){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String begin = "" ;
        String end="";
        if (from.getStatus()==0){
            from.setToday(0);
            return;
        } else if (from.getStatus() == 1){
            begin = simpleDateFormat.format(DateTimeUtil.getDayBegin());
            end = simpleDateFormat.format(DateTimeUtil.getDayEnd());
            conversionPeriod(from);
        }else if (from.getStatus() == 2){
             begin = simpleDateFormat.format(DateTimeUtil.getBeginDayOfTomorrow());
            end = simpleDateFormat.format(DateTimeUtil.getEndDayOfTomorrow());
            conversionPeriod(from);
        }else if (from.getStatus() == 3){
            begin = simpleDateFormat.format(DateTimeUtil.getBeginDayOfWeek());
            end = simpleDateFormat.format(DateTimeUtil.getBeginDayOfWeek());
            from.setToday(-1);//无需判断今天为周几
        }else if (from.getStatus() == 4){
            begin = simpleDateFormat.format(DateTimeUtil.getBeginDayOfMonth());
            end = simpleDateFormat.format(DateTimeUtil.getEndDayOfMonth());
            from.setToday(-1);
        }else if (from.getStatus() == 5){
            begin = simpleDateFormat.format(DateTimeUtil.getBeginDayOfYear());
            end = simpleDateFormat.format(DateTimeUtil.getEndDayOfYear());
            from.setToday(-1);
        }else if (from.getStatus() == 6){
            begin = simpleDateFormat.format(new Date());
            from.setBeginDate(begin);
            return;
        }
        from.setBeginDate(begin);
        from.setEndDate(end);




    }

    public void conversionPeriod(promotionNowFrom from){
        if ("星期一".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(1);
        }else if ("星期二".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(2);
        }else if ("星期三".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(3);
        }else if ("星期四".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(4);
        }else if ("星期五".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(5);
        }else if ("星期六".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(6);
        }else if ("星期日".equals(DateTimeUtil.getCurrDayOfWeek())){
            from.setToday(7);
        }
    }


}
